package com.iseastar.dianxiaosan.stamp;

import android.os.Bundle;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.widget.ListView;

import com.iseastar.BaseActivity2;
import com.iseastar.dianxiaosan.model.StampHistoryBean;
import com.iseastar.guojiang.consts.MsgID;
import com.iseastar.guojiang.logic.AppHttp;
import com.iseastar.guojiang.logic.JSON;
import com.iseastar.guojiang.model.ReqResult;
import com.shouhuobao.station.R;

import java.util.ArrayList;
/**
 * 邮票购买记录
 */
public class StampBuyHistoryActivity extends BaseActivity2 {
    private ListView mListView;
    private StampHistoryAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        showLoadingDialog(null);
        AppHttp.getInstance().stampBuyHistoty();
    }

    @Override
    protected void findViewById() {
        setContentView(R.layout.stamp_history_list);
        super.findViewById();
        getAppTitle().setCommonTitle("购买记录");
        mListView = (ListView) findViewById(R.id.listview);
        mSwipeLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh() {
                showLoadingDialog("");
                AppHttp.getInstance().stampBuyHistoty();
            }
        });

        adapter = new StampHistoryAdapter(null, this);
        mListView.setAdapter(adapter);
    }

    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            case MsgID.stamp_history:
                cancelLoadingDialog();
                onRefreshComplete();
                final ReqResult<StampHistoryBean> result = JSON.parseList(msg.obj, StampHistoryBean.class);
                if (checkLoginStatus(result)) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            ArrayList<StampHistoryBean> resultList = result.getResultList();
                            adapter.setItems(resultList, true);
                        }
                    });

                } else {
                    showToast(result.getMessage());
                }
                return true;
            default:
                return super.handleMessage(msg);
        }
    }
}
